package com.antis.olsl.library.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.antis.olsl.library.R;
import com.antis.olsl.library.response.SalesroomOrWarehouseInfo;

import java.util.ArrayList;

public class ChooseWarehouseView extends LinearLayout implements View.OnClickListener {
    RelativeLayout rlChooseSalesrooms;
    TextView tvSalesroomsValue;
    LinearLayout llStatus;
    TextView tvStatus;
    ImageView imgTriangle;
    LinearLayout llInfo;
    TextView tvManager;
    TextView tvAddress;
    TextView tvPhone;
    boolean isSalesroom = true;
    ArrayList<SalesroomOrWarehouseInfo> salesWarehouseInfos = new ArrayList<>();
    ChooseWarehouseListener chooseWarehouseListener;
    public ChooseWarehouseView(Context context) {
        super(context);
    }
    public ChooseWarehouseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        rlChooseSalesrooms = findViewById(R.id.rl_chooseSalesrooms);
        tvSalesroomsValue = findViewById(R.id.tv_salesroomsValue);
        llStatus = findViewById(R.id.ll_status);
        tvStatus = findViewById(R.id.tv_status);
        imgTriangle = findViewById(R.id.img_triangle);
        llInfo = findViewById(R.id.ll_info);
        tvManager = findViewById(R.id.tv_manager);
        tvAddress = findViewById(R.id.tv_address);
        tvPhone = findViewById(R.id.tv_phone);

        rlChooseSalesrooms.setOnClickListener(this);
        llStatus.setOnClickListener(this);
        tvPhone.setOnClickListener(this);
    }

    public void setChooseWarehouseListener(ChooseWarehouseListener chooseWarehouseListener) {
        this.chooseWarehouseListener = chooseWarehouseListener;
    }

    public void setWarehouseInfos(ArrayList<SalesroomOrWarehouseInfo> salesWarehouseInfos) {
        this.salesWarehouseInfos = salesWarehouseInfos;
        if (salesWarehouseInfos == null || salesWarehouseInfos.size() == 0) {
            setSalesroom(isSalesroom);
        } else {
            SalesroomOrWarehouseInfo salesroomOrWarehouseInfo = salesWarehouseInfos.get(0);
            llStatus.setVisibility(VISIBLE);
            llInfo.setVisibility(GONE);

            tvSalesroomsValue.setText(salesroomOrWarehouseInfo.getWarehouseName());
            tvStatus.setText(salesroomOrWarehouseInfo.getStatus());
            tvManager.setText(salesroomOrWarehouseInfo.getLinkMan());
            tvAddress.setText(salesroomOrWarehouseInfo.getWarehouseAddr());
            tvPhone.setText(salesroomOrWarehouseInfo.getLinkManTel());
        }
    }

    public void setSalesroom(boolean salesroom) {
        isSalesroom = salesroom;
        tvSalesroomsValue.setText("全部仓库");
        llStatus.setVisibility(GONE);
        llInfo.setVisibility(GONE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_status) {
            imgTriangle.setRotation(llInfo.getVisibility() == VISIBLE ? 0 : 180);
            llInfo.setVisibility(llInfo.getVisibility() == VISIBLE ? GONE : VISIBLE);
        } else if (id == R.id.rl_chooseSalesrooms) {
            if (chooseWarehouseListener != null) {
                chooseWarehouseListener.clickChooseListener();
            }
        } else if (id == R.id.tv_phone) {
            if (chooseWarehouseListener != null) {
                chooseWarehouseListener.clickPhone(tvPhone.getText().toString());
            }
        }
    }

    public interface ChooseWarehouseListener {
        void clickChooseListener();

        void clickPhone(String phone);
    }
}
